#!/bin/bash

args=()
until [ -z "$1" ]; do
  case "$1" in
    -t|--tmux) t=1; shift ;;
    --) shift ; break ;;
    -*) echo "invalid option $1" 1>&2 ; shift ;; # or, error and exit 1 just like getopt does
    *) args+=("$1") ; shift ;;
  esac
done

args+=("$@")
for arg in "${args[@]}" ; do
    # Switch to mobile reddit
    url=${arg/http:\/\/reddit.com/http:\/\/m.reddit.com}
    url=${url/http:\/\/www.reddit.com/http:\/\/m.reddit.com}
    # Fix double backslash error in comment uri for mobile reddit
    url=${url/\/\/comments/\/comments}
    if [[ $t == "1" ]]; then
        tmux new-window 'w3m "'${url}'"'
    else
        w3m "${url}"
    fi
done
